package machine;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;

import data.Card;

public class Drop {
	private int C = 0;
	private int N = 0;
	private int R = 0;
	private int S = 0;
	private int M = 0;
	private int U = 0;
	
	private Vector<Card> database;
	private Random rand;
	public Drop(Vector<Card> db) {
		database = db;
		rand = new Random();
		interpret(database);
	}
	public Card produce() {
		int rst = rand.nextInt(10000);
		if (rst < 1) {			//0.01% to get Unique
			return database.get(0);
		} else if (rst < 11) {	//0.10% to get Mysterious
			return database.get(rand.nextInt(M)+U);
		} else if (rst < 36) {	//0.25% to get Super Rare
			return database.get(rand.nextInt(S)+M+U);
		} else if (rst < 86) {	//0.50% to get Rare
			return database.get(rand.nextInt(R)+S+M+U);
		} else if (rst < 186) {	//1.00% to get Normal
			return database.get(rand.nextInt(N)+R+S+M+U);
		} else {
			return database.get(rand.nextInt(C)+N+R+S+M+U);
		}
	}
	private void interpret(Vector<Card> db) {
		Collections.sort(db, new Comparator<Card>(){
			@Override
			public int compare(Card c1, Card c2) {
				return Integer.valueOf(c2.rank).compareTo(Integer.valueOf(c1.rank));
			}
		});
		for (Iterator<Card> i = db.iterator();i.hasNext();) {
			switch (i.next().rank) {
			case 0: C++;break;
			case 1: N++;break;
			case 2: R++;break;
			case 3: S++;break;
			case 4: M++;break;
			case 5: U++;break;
			}
		}
	}
}